package com.iwinding.ladygg.utils;

import android.content.Context;

import com.amap.api.location.AMapLocationClientOption;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationListener;

/**
 * Created by 82218 on 2017/6/22.
 */

public class AMMapHelper {


    //CrashHandler实例
    private static AMMapHelper INSTANCE = new AMMapHelper();
    //程序的Context对象
    private Context mContext;

    private AMapLocation mLocationInfo = null;

    private OnLocationListener onLocationListener = null;

    /**
     * 保证只有一个AMMapHelper实例
     */
    private AMMapHelper() {
    }

    public static AMMapHelper getInstance() {
        return INSTANCE;
    }

    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    //声明定位回调监听器
    public AMapLocationListener mLocationListener = new AMapLocationListener() {

        @Override
        public void onLocationChanged(AMapLocation amapLocation) {
            if (amapLocation != null) {
                if (amapLocation.getErrorCode() == 0) {
                    //解析定位结果
                    int locType = amapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见定位类型表
                    switch (locType) {
                        case 1://GPS定位结果
                        case 2://前次定位结果
                        case 4://缓存定位结果
                        case 5://Wifi定位结果
                        case 6://基站定位结果
                        case 8://离线定位结果
                            mLocationInfo = amapLocation;
                            setSpeed(mLocationInfo.getSpeed());
                            break;
                    }
                }
            }
//            if (null != onLocationListener) {
//                onLocationListener.onLocation(mLocationInfo);
//            }
//            if (mLocationClient != null)
//                mLocationClient.stopLocation();
        }
    };


    public void init(Context mContext) {
        this.mContext = mContext;
        initLoaction();
    }

    public AMapLocation getmLocationInfo() {
        return mLocationInfo;
    }

    public void startLocation() {
        //启动定位
        initLoaction();
        if (mLocationClient != null)
            mLocationClient.startLocation();
    }

//    public void stopLocation() {
//        if (mLocationClient != null) {
//            mLocationClient.stopLocation();
//            mLocationClient.unRegisterLocationListener(mLocationListener);
//        }
//        mLocationClient = null;
//    }


    public void setmLocationInfo(AMapLocation mLocationInfo) {
        this.mLocationInfo = mLocationInfo;
    }

    private void initLoaction() {
        if (mLocationClient == null) {
            //初始化定位
            mLocationClient = new AMapLocationClient(mContext.getApplicationContext());
            //设置定位回调监听
            mLocationClient.setLocationListener(mLocationListener);
            //初始化
            initLocationParam();
        }
    }

    private void initLocationParam() {
        AMapLocationClientOption option = new AMapLocationClientOption();
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
//        option.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系，bd09ll，百度坐标
        option.setOnceLocation(true);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于span才是有效的
        option.setOnceLocationLatest(true);
        option.setGpsFirst(true);
        option.setNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setInterval(1000);
        option.setWifiActiveScan(false);//设置是否强制刷新WIFI

        option.setMockEnable(true);//设置是否允许模拟位置,默认为true，允许模拟位置
        option.setHttpTimeOut(20000);//定位请求超时时间
        option.setLocationCacheEnable(true);//关闭缓存机制

        mLocationClient.setLocationOption(option);
    }

    interface OnLocationListener {
        void onLocation(AMapLocation mLocationInfo);
    }


    boolean isFrist = true;

    //
    private void setSpeed(float speed) {
        LogUtil.i("@hzy", "----------speed:" + speed);
        if (null == mLocationClient) {
            return;
        }
        AMapLocationClientOption option = new AMapLocationClientOption();
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
//        option.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系，bd09ll，百度坐标
        option.setOnceLocation(false);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于span才是有效的
        option.setOnceLocationLatest(false);
        option.setNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setInterval(1000);
        option.setWifiActiveScan(false);//设置是否强制刷新WIFI

        option.setMockEnable(true);//设置是否允许模拟位置,默认为true，允许模拟位置
        option.setHttpTimeOut(20000);//定位请求超时时间
        option.setLocationCacheEnable(false);//关闭缓存机制
        if (speed >= 0 && speed < 0.3) {
            option.setInterval(10000);
        } else if (speed >= 0.3 && speed < 1.5) {
            option.setInterval(5000);
        } else if (speed >= 1.5) {
            option.setInterval(2000);
        }
        mLocationClient.setLocationOption(option);
        if (isFrist) {
            mLocationClient.stopLocation();
            mLocationClient.setLocationListener(mLocationListener);
            mLocationClient.startLocation();
        }
        isFrist = false;
    }

}
